/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dto.CarCancelDTO;
import east2west.dto.CarOrderDTO;
import east2west.dto.CarOrderDetailDTO;
import east2west.dto.CarsDTO;
import east2west.dto.CustomerDTO;
import east2west.dto.JobDTO;
import east2west.dto.TourCancelDTO;
import east2west.dto.TourOrderDTO;
import east2west.dto.TourOrderDetailDTO;
import east2west.service.CarCancelService;
import east2west.service.CarOrderDetailService;
import east2west.service.CarOrderService;
import east2west.service.CarService;
import east2west.service.CustomerService;
import east2west.service.JobService;
import east2west.service.TourService;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author anhtuan
 */
public class CartViewAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
//    private final static String SEARCH = "search";
//    private final static String CARTCAR = "cartcar";
//    private final static String CARTTOUR = "carttour";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward getCart(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();

        if (session.getAttribute("cartCar") != null) {
            JobService serJob = new JobService();
            ArrayList<JobDTO> listJob = serJob.readByAll();
            request.setAttribute("listJob", listJob);
            request.setAttribute("page", "carbook.page");
            request.setAttribute("pages", "Car Book");
        } else if (session.getAttribute("cartTour") != null) {
            request.setAttribute("page", "booking.page");
            // request.setAttribute("page", "carttour.page");
        } else {
            request.setAttribute("page", "cartsearch.page");
        }
        return mapping.findForward(SUCCESS);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward search(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        //get parameter
        int code = Integer.parseInt(request.getParameter("code"));
        String email = request.getParameter("email");
        String option = request.getParameter("option");
        float money = 0;
        //kiem tra option new la car
        if (option.equals("car")) {
            //find carorder  by code
            CarOrderService serCarOrder = new CarOrderService();
            CarOrderDTO carOrderDTO = serCarOrder.findById(code);
            //kiem tra carorder
            if (carOrderDTO == null) {
                //thong bao nhap sai code
                request.setAttribute("mess", "code cua ban nhap bi sai roi");
                request.setAttribute("page", "cartcar.page");
                return mapping.findForward(SUCCESS);
            } else {
                //check email
                CustomerService serCust = new CustomerService();
                CustomerDTO custDTO = serCust.findById(carOrderDTO.getCustId());
                if (custDTO.getEmail().trim().equals(email)) {
                    //neu la dung thi liet ke tat ca cac orderdetail cua id nay
                    //sao cho van con co 1 orderdetailid chu co trong bang carcancel
                    //kiem tra bang carcancel ko co du lieu

                    CarOrderDetailService serCarOrderDetail = new CarOrderDetailService();

                    //neu co du lieu thi
                    ArrayList<CarOrderDetailDTO> listCarOrderDetail = serCarOrderDetail.findByOrderDetailNotCancel(carOrderDTO.getOrderId());

                    if (listCarOrderDetail.isEmpty()) {
                        request.setAttribute("mess", "ban ko the huy order nay nua");
                        request.setAttribute("page", "cartcar.page");
                        return mapping.findForward(SUCCESS);
                    } else {
                        CarService serCar = new CarService();
                        ArrayList<CarOrderDetailDTO> listCarOrderDetail1 = new ArrayList<CarOrderDetailDTO>();
                        for (Iterator<CarOrderDetailDTO> it = listCarOrderDetail.iterator(); it.hasNext();) {
                            CarOrderDetailDTO carOrderDetailDTO = it.next();
                            CarCancelService serCarCancel = new CarCancelService();
                            CarCancelDTO carCancelDTO = serCarCancel.findByCarOrderDetailid(carOrderDetailDTO.getCarOderDetailId());
                            if (carCancelDTO == null) {
                                CarsDTO carsDTO = serCar.findById(carOrderDetailDTO.getCarId());
                                carOrderDetailDTO.setCarsDTO(carsDTO);
                                money += carOrderDetailDTO.getTotal();
                                listCarOrderDetail1.add(carOrderDetailDTO);
                            }

                        }
                        if (listCarOrderDetail1.isEmpty()) {
                            request.setAttribute("mess", "ban ko the huy order nay nua");
                            request.setAttribute("page", "cartcar.page");
                            return mapping.findForward(SUCCESS);
                        } else {
                            request.setAttribute("listCarOrderDetail", listCarOrderDetail1);
                            request.setAttribute("carOrderDTO", carOrderDTO);
                            request.setAttribute("money", money);
                            request.setAttribute("custDTO", custDTO);
                            request.setAttribute("page", "cartcar.page");
                            return mapping.findForward(SUCCESS);
                        }


                    }
                } else {
                    //thong bao email cua ban ko phai la chu cua order nay
                    request.setAttribute("mess", "xin loi ban ko phai la chu cua order nay");
                    request.setAttribute("page", "cartcar.page");
                    return mapping.findForward(SUCCESS);
                }
            }
        } else {
            //find tour order by code
            TourService serTour = new TourService();
            TourOrderDTO tourOrderDTO = serTour.findByTourOrderId(code);
            //check if tourorderDTO == null
            if (tourOrderDTO == null) {
                //thong bao sai code
                request.setAttribute("mess", "code cua ban nhap bi sai roi");
                request.setAttribute("page", "carttour.page");
                return mapping.findForward(SUCCESS);
            } else {
                //check email
                CustomerService serCust = new CustomerService();
                CustomerDTO custDTO = serCust.findById(tourOrderDTO.getCustId());
                if (custDTO.getEmail().trim().equals(email)) {
                    //tim kiem tourorderdetail

                    ArrayList<TourOrderDetailDTO> listTourOrderDetail = serTour.findTourOrderDetailCancel(tourOrderDTO.getOrderId());
                    if (listTourOrderDetail.isEmpty()) {
                        request.setAttribute("mess", "ban ko the huy order nay nua");
                        request.setAttribute("page", "carttour.page");
                        return mapping.findForward(SUCCESS);
                    } else {
                        ArrayList<TourOrderDetailDTO> listTourOrderDetail1 = new ArrayList<TourOrderDetailDTO>();
                        for (Iterator<TourOrderDetailDTO> it = listTourOrderDetail.iterator(); it.hasNext();) {
                            TourOrderDetailDTO tourOrderDetailDTO = it.next();
                            TourService tourSer = new TourService();
                            TourCancelDTO cancelDTO = tourSer.findByOrderDetail(tourOrderDetailDTO.getTourOrderDetailId());
                            if (cancelDTO == null) {
                                listTourOrderDetail1.add(tourOrderDetailDTO);
                                money += tourOrderDetailDTO.getAmount();
                            }

                        }
                        if (listTourOrderDetail1.isEmpty()) {
                            request.setAttribute("mess", "ban ko the huy order nay nua");
                            request.setAttribute("page", "carttour.page");
                            return mapping.findForward(SUCCESS);
                        } else {
                            request.setAttribute("listTourOrderDetail", listTourOrderDetail1);
                            //request.setAttribute("carOrderDTO", carOrderDTO);
                            request.setAttribute("money", money);
                            request.setAttribute("custDTO", custDTO);
                            request.setAttribute("page", "carttour.page");
                            return mapping.findForward(SUCCESS);
                        }
                    }

                } else {
                    request.setAttribute("mess", "ban ko phai la chu cua order nay");
                    request.setAttribute("page", "carttour.page");
                    return mapping.findForward(SUCCESS);
                }

            }
        }

    }
}
